var lastValue = "", nodeList = [], fontCss = {};
var tloadinit;
ztreeAsyncPost = {
	treeIframeDoSubmit : function(postUrl,formid){
		if(validateForm.form()){
			ztreeAsyncPost.doSubmitForumTreeFormPost(postUrl,formid);
			return true;
		}
		return false;
	},
	doSubmitForumTreePost : function(postUrl) {
		var array = {};
		if(!postUrl)
			postUrl = "saveAllotRoleAuthorityTreeJson";
		array['rid'] = $("#rid").val();
		array['id'] = $("#checkIds").val();
		var tload = top.layer.load(0, {
			shade : [ 0.5, '#fff' ]
		}); // 0代表加载的风格，支持0-2
		$.ajax({
			headers : {
				'Accept' : 'application/json',
				'Content-Type' : 'application/json'
			},
			type : "post",
			data : JSON.stringify(array),
			url :  postUrl,
			async : true,
			dataType : "json"
		}).done(function(data) {
			// 成功回调，
			if (data != null) {
				top.layer.close(tload);
				$("#asyncResult").val(data.ret);
				$("#asyncResultError").val(data.msg);
			}
		}).fail(function() {
			// 失败回调
			top.layer.close(tload);
			$("#asyncResult").val(-110);
		});
		return true;
	},
	doSubmitForumTreeFormPost : function(postUrl,formid) {
		var array = {};
		var arrs = $('#' + formid).serializeArray();
		console.log("arrs>>"+JSON.stringify(arrs));
		for ( var item in arrs) {
			if(arrs[item].name!='ctime' && arrs[item].name!='texts'){
				array[arrs[item].name] = arrs[item].value;
			}
		}
		var tload = top.layer.load(0, {
			shade : [ 0.5, '#fff' ]
		}); // 0代表加载的风格，支持0-2
		$.ajax({
			headers : {
				'Accept' : 'application/json',
				'Content-Type' : 'application/json'
			},
			type : "post",
			data : JSON.stringify(array),
			url :  postUrl,
			async : true,
			dataType : "json"
		}).done(function(data) {
			// 成功回调，
			if (data != null) {
				console.log(data.data.mid+"===data>>"+JSON.stringify(data));
				top.layer.close(tload);
				$("#asyncResult").val(data.ret);
				if(data.data)
					$("#asyncResultData").val(data.data.mid);
				$("#asyncResultError").val(data.msg);
			}
		}).fail(function() {
			// 失败回调
			top.layer.close(tload);
			$("#asyncResult").val(-110);
		});
		return true;
	},
	openTreeDefault : function(openUrl,postUrl,titles){
		var titles = title;//"给【"+rolename+"】分配权限";
		top.layer.open({
		  type: 2,
		  title: titles,
		  shade: 0.5,//背景色深度
		  fixed: false, //不固定
		  maxmin: true,
		  anim : 1,
		  shadeClose: false,//点击其它区域关闭
		  content:  openUrl,//'ztreeAsyncPostLayer.do?rid='+id,
		  area: ['300px' , '650px'],
		  btn : ["提交",'关闭'],
		  yes : function(index,layero){
			  var body = top.layer.getChildFrame('body',index);
				var iframeWin = layero.find('iframe')[0];//得到iframe页的窗口对像，执行iframe页的方法，iframeWin.method();
				var values = iframeWin.contentWindow.ztreeAsyncPost.doSubmitForumTreePost(postUrl);
				if(values){
					var result = 0;
					var count = 0;
					var tmsg = top.layer.msg('数据提交中.......', {
						time : 100000,
						icon : 6
					});
					var tload = top.layer.load(0, {
						shade : [ 0.5, '#fff' ]
					}); // 0代表加载的风格，支持0-2
					ztreeAsyncPost.asyncSaveFormTreeLoadClose(iframeWin, index, result, 1000, count,tmsg, tload);
				}
		  },
		  end: function(index){
			  
		  }
		});
	},
	openTreeByWH : function(openUrl,postUrl,titles,w,h){
		//var titles = "给【"+rolename+"】分配权限";
		top.layer.open({
		  type: 2,
		  title: titles,
		  shade: 0.5,//背景色深度
		  fixed: false, //不固定
		  maxmin: true,
		  anim : 1,
		  shadeClose: false,//点击其它区域关闭
		  content:  openUrl,//'ztreeAsyncPostLayer.do?rid='+id,
		  area: [w+'px' , h+'px'],
		  btn : ["提交",'关闭'],
		  yes : function(index,layero){
			  var body = top.layer.getChildFrame('body',index);
				var iframeWin = layero.find('iframe')[0];//得到iframe页的窗口对像，执行iframe页的方法，iframeWin.method();
				var values = iframeWin.contentWindow.ztreeAsyncPost.doSubmitForumTreePost(postUrl);
				if(values){
					var result = 0;
					var count = 0;
					var tmsg = top.layer.msg('数据提交中.......', {
						time : 100000,
						icon : 6
					});
					var tload = top.layer.load(0, {
						shade : [ 0.5, '#fff' ]
					}); // 0代表加载的风格，支持0-2
					ztreeAsyncPost.asyncSaveFormTreeLoadClose(iframeWin, index, result, 1000, count,tmsg, tload);
				}
		  },
		  end: function(index){
			  
		  }
		});  
	},
	
	browseTreeViewDefault : function(openUrl,titles){
		//var titles = title;//"浏览角色【"+rolename+"】权限";
		top.layer.open({
		  type: 2,
		  title: titles,
		  shade: 0.5,//背景色深度
		  fixed: false, //不固定
		  maxmin: true,
		  anim : 1,
		  shadeClose: false,//点击其它区域关闭
		  content:  openUrl,//'ztreeAsyncPostLayer.do?rid='+rid,
		  area: ['300px' , '650px'],
		  btn : ['关闭'],
		  end: function(index){
			  
		  }
		});  
	},
	browseTreeViewByWH : function(openUrl,titles,w,h){
		//var titles = title;//"浏览角色【"+rolename+"】权限";
		top.layer.open({
		  type: 2,
		  title: titles,
		  shade: 0.5,//背景色深度
		  fixed: false, //不固定
		  maxmin: true,
		  anim : 1,
		  shadeClose: false,//点击其它区域关闭
		  content:  openUrl,//'ztreeAsyncPostLayer.do?rid='+rid,
		  area: [w+'px' , h+'px'],
		  btn : ['关闭'],
		  end: function(index){
			  
		  }
		});  
	},
	openTreeSelectedToParentDefault : function(checkIdsText,checkNamesText,showText,openUrl){
		//打开树后，选择节点返回给父页面
		var ids = $("#"+checkIdsText).val();
		var checkName = $("#"+checkNamesText).val();
		ids = functionUtil.replaceAllValue(ids,"#","_");
		checkName = functionUtil.replaceAllValue(checkName,"#","_");
		var titles = "选择权限";
		top.layer.open({
		  type: 2,
		  title: titles,
		  shade: 0.5,//背景色深度
		  fixed: false, //不固定
		  maxmin: true,
		  anim : 1,
		  shadeClose: false,//点击其它区域关闭
		  content:  'allotMenuAuthority.do?ids='+ids+"&checkName="+checkName,
		  area: ['300px' , '380px'],
		  btn : ["提交",'关闭'],
		  yes : function(index,layero){
			  var body = top.layer.getChildFrame('body',index);
				var iframeWin = layero.find('iframe')[0];//得到iframe页的窗口对像，执行iframe页的方法，iframeWin.method();
				var values = iframeWin.contentWindow.selectedAuthority.returnIsCheck();
				if(values!=''){
					//表单提交后，先延时1秒去取异步结果，如果还不成功，再延时3秒去取结果，正常的逻辑时间够了，如果是有大内容就得再回5秒或10秒
					$("#"+checkIdsText).val(values.ids);
					$("#"+checkNamesText).val(values.checkName);
					$("#"+showText).html(values.showname);
					top.layer.close(index);
				}
		  },
		  end: function(index){
			  
		  }
		});  
	},
	openTreeSelectedToParentByWH : function(checkIdsText,checkNamesText,showText,openUrl,w,h){
		//打开树后，选择节点返回给父页面
		var ids = $("#"+checkIdsText).val();
		var checkName = $("#"+checkNamesText).val();
		ids = functionUtil.replaceAllValue(ids,"#","_");
		checkName = functionUtil.replaceAllValue(checkName,"#","_");
		var titles = "选择权限";
		top.layer.open({
		  type: 2,
		  title: titles,
		  shade: 0.5,//背景色深度
		  fixed: false, //不固定
		  maxmin: true,
		  anim : 1,
		  shadeClose: false,//点击其它区域关闭
		  content:  openUrl+'?ids='+ids+'&checkName='+checkName,
		  area: [w+'px' , h+'px'],
		  btn : ["提交",'关闭'],
		  yes : function(index,layero){
			  var body = top.layer.getChildFrame('body',index);
				var iframeWin = layero.find('iframe')[0];//得到iframe页的窗口对像，执行iframe页的方法，iframeWin.method();
				var values = iframeWin.contentWindow.ztreeAsyncPost.returnArrayIsCheckToParent(checkIdsText,checkNamesText);
				console.log(values);
				if(values!=''){
					//表单提交后，先延时1秒去取异步结果，如果还不成功，再延时3秒去取结果，正常的逻辑时间够了，如果是有大内容就得再回5秒或10秒
					$("#"+checkIdsText).val(values.ids);
					$("#"+checkNamesText).val(values.checkName);
					$("#"+showText).html(values.showname);
					top.layer.close(index);
				}
		  },
		  end: function(index){
			  
		  }
		});  
	},
	
	addEditTreeIframeNodeByWH : function(openUrl,postUrl,formid,titles,w,h,refreshUrl,refreshNowUrl){
		//var titles = "给【"+rolename+"】分配权限";
		top.layer.open({
		  type: 2,
		  title: titles,
		  shade: 0.5,//背景色深度
		  fixed: false, //不固定
		  maxmin: true,
		  anim : 1,
		  shadeClose: false,//点击其它区域关闭
		  content:  openUrl,//'ztreeAsyncPostLayer.do?rid='+id,
		  area: [w+'px' , h+'px'],
		  btn : ["提交",'关闭'],
		  yes : function(index,layero){
			var body = top.layer.getChildFrame('body',index);
			var iframeWin = layero.find('iframe')[0];//得到iframe页的窗口对像，执行iframe页的方法，iframeWin.method();
			var values = iframeWin.contentWindow.ztreeAsyncPost.treeIframeDoSubmit(postUrl,formid);
			if(values){
				var result = 0;
				var count = 0;
				var tmsg = top.layer.msg('数据提交中.......', {
					time : 100000,
					icon : 6
				});
				var tload = top.layer.load(0, {
					shade : [ 0.5, '#fff' ]
				}); // 0代表加载的风格，支持0-2
				ztreeAsyncPost.asyncSaveFormTreeNodeIframeClose(iframeWin, index, result, 1000, count,tmsg, tload,refreshUrl,refreshNowUrl);
			}
		  },
		  end: function(index){
			  
		  }
		});  
	},
	asyncSaveFormTreeLoadClose : function(iframeWin, index, result, time, count,tmsg, tload) {
		return setTimeout(function() {
			count++;
			result = iframeWin.contentWindow.ztreeAsyncPost.asyncTreeCallResult();
			if (result == 1) {
				top.layer.msg('数据提交成功.......', {
					time : 2000,
					icon : 6
				});
				top.layer.close(index);
				top.layer.close(tmsg);
				top.layer.close(tload);
			} else if(result == -1){
				if (count > 5) {
					top.layer.alert('数据提交失败....', {
						skin : 'layui-layer-lan',
						closeBtn : 0,
						anim : 4,
						icon : 5
					});
					top.layer.close(index);
					top.layer.close(tmsg);
					top.layer.close(tload);
				} else {
					ztreeAsyncPost.asyncSaveFormTreeLoadClose(iframeWin, index, result, (time + 1000), count,tmsg, tload);
				}
			}else if(result==-110){
				top.layer.close(tload);
				top.layer.alert('访问服务器接口出错....', {
					skin : 'layui-layer-lan',
					closeBtn : 0,
					anim : 4,
					icon : 5
				});
			}else{
				top.layer.close(tload);
				var error = iframeWin.contentWindow.ztreeAsyncPost.asyncTreeCallResultError();
				top.layer.alert('操作失败。错误码：'+result+" 错误信息："+error, {
					skin : 'layui-layer-lan',
					closeBtn : 0,
					anim : 4,
					icon : 5
				});
			}
		}, time);
	},
	asyncSaveFormTreeNodeIframeClose : function(iframeWin, index, result, time, count,tmsg, tload,refreshUrl,refreshNowUrl) {
		return setTimeout(function() {
			count++;
			result = iframeWin.contentWindow.ztreeAsyncPost.asyncTreeCallResult();
			if (result == 1) {
				top.layer.msg('数据提交成功.......', {
					time : 2000,
					icon : 6
				});
				top.layer.close(index);
				top.layer.close(tmsg);
				top.layer.close(tload);
				var data = iframeWin.contentWindow.ztreeAsyncPost.asyncTreeCallResultData();
				setTimeout(function(){
					$('#tree', parent.document).attr("src", refreshUrl+"?data=" + Math.random());
					window.location.href=refreshNowUrl+data; 
				} ,500);
			} else if(result == -1){
				if (count > 5) {
					top.layer.alert('数据提交失败....', {
						skin : 'layui-layer-lan',
						closeBtn : 0,
						anim : 4,
						icon : 5
					});
					top.layer.close(index);
					top.layer.close(tmsg);
					top.layer.close(tload);
				} else {
					ztreeAsyncPost.asyncSaveFormTreeNodeIframeClose(iframeWin, index, result, (time + 1000), count,tmsg, tload,refreshUrl,refreshNowUrl);
				}
			}else if(result==-110){
				top.layer.close(tload);
				top.layer.alert('访问服务器接口出错....', {
					skin : 'layui-layer-lan',
					closeBtn : 0,
					anim : 4,
					icon : 5
				});
			}else{
				top.layer.close(tload);
				var error = iframeWin.contentWindow.ztreeAsyncPost.asyncTreeCallResultError();
				top.layer.alert('操作失败。错误码：'+result+" 错误信息："+error, {
					skin : 'layui-layer-lan',
					closeBtn : 0,
					anim : 4,
					icon : 5
				});
			}
		}, time);
	},
	ztreeIframeNodeDelete : function(id,delurl,treeid,treeurl,treeinfoid){
		var content = "<span style=\"color:red\">您确定要删除吗？</span>";
		top.layer.confirm(content,{
			btn:['删除','取消'],
			icon : 5
		},function(){
			// 删除动态
			var tload = top.layer.load(0, {
				shade : [ 0.5, '#fff' ]
			});
			status = status == 1 ? 0 : 1;
			$.ajax({
				type : "post",
				data : "id=" + id,
				url : delurl,
				async : true,
				dataType : "json",
			}).done(function(data) {
				top.layer.close(tload);
				if (data != null) {
					if (data.ret == 1) {
						top.layer.msg('删除成功.......', {
							time : 1000,
							icon : 6
						});
						$(treeid, parent.document).attr("src", treeurl + Math.random());
						$(treeinfoid, parent.document).attr("src", "");
					} else {
						top.layer.alert('删除失败....' + data.msg, {
							skin : 'layui-layer-lan',
							closeBtn : 0,
							anim : 4,
							icon : 5
						});
					}
				}
				$("body").hideLoading();
			}).fail(function() {
				top.layer.close(tload);
				top.layer.alert('删除失败....', {
					icon : 5,
					skin : 'layui-layer-lan',
					closeBtn : 0,
					anim : 4
				});
			});
		},function(){
			
		});
	},
	asyncTreeCallResult : function(){
		return $("#asyncResult").val();
	},
	asyncTreeCallResultError : function(){
		return $("#asyncResultError").val();
	},
	asyncTreeCallResultData : function(){
		return $("#asyncResultData").val();
	},
	returnArrayIsCheckToParent : function(checkIdsText,checkNamesText){
		var array={};
		var ids = $("#"+checkIdsText).val();
		var v = $("#"+checkNamesText).val();
		if(ids!=''){
			var html ='';
			var arrids = ids.split('#',-1);
			var arrname = v.split('#',-1);
			$.each(arrids, function(i, id) {
				if(i%2==0){
					html +='<button type="button" id="authority_'+id+'" class="btn btn-xs btn-info" style="margin-bottom:5px;margin-right:15px;">【'+arrname[i]+'】&nbsp;</button><span class="btn-danger circle_del" id="authority_span_'+id+'" title="移除" onclick="removeAu('+id+',\''+arrname[i]+'\')">x</span>';
				}else{
					html +='<button type="button" id="authority_'+id+'" class="btn btn-xs btn-success" style="margin-bottom:5px;margin-right:15px;">【'+arrname[i]+'】&nbsp;</button><span class="btn-danger circle_del" id="authority_span_'+id+'" title="移除" onclick="removeAu('+id+',\''+arrname[i]+'\')">x</span>';
				}
			});
			array.ids=ids;
			array.showname = html;
			array.checkName = v;
			return array;
		}
		return "";
	}
}
